<?php

namespace App\Datas;

use App\Tools\FunTool;
use Illuminate\Support\Facades\DB;

class ModelStyle extends Base
{
    const STATUS_ANLE = 0;

    public $tableName = "model_style";

	public $sVersion='1.0.2';
	public $sPlatform='1';//1:ios 2:安卓

    private static $instance = null;

    private function __construct()
    {
		$this->sVersion = $_SERVER['HTTP_AIVERSION'] ?? '1.0.2';
		$this->sPlatform = $_SERVER['HTTP_PLATFORM'] ?? 1;

		//if(1 == $this->sPlatform && '1.8.0' == $this->sVersion) {
		//	$this->tableName = 'model_style_special';
		//}
		// if(2 == $this->sPlatform) {
		 	$this->tableName = 'model_style_special';
		// }
    }

    public static function getInstance() {
        if (empty(self::$instance) && !(self::$instance instanceof ModelStyle)) {
            self::$instance = new self();
        }
        return self::$instance;
    }


    /**
     * 格式化列表
     * @param $list
     * @return mixed
     */
    public function formatList($list) {
        foreach ($list as &$item) {
            $item = (array)$item;
            $this->formatItem($item,"!webp");
        }
        return $list;
    }

    /**
     * 格式化item
     * @param $item
     * @return void
     */
    public function formatItem(&$item,$imgStyle = "") {
        if (array_key_exists("img",$item)) {
            $item["img"] = env("APP_FILES_URL")."/".$item["img"].$imgStyle;
            $item["height"] = (int)env("IMG_HEIGHT");
            $item["width"] = (int)env("IMG_WIDTH");
        }
    }

    public function getCountByModelId($model_id) {
        $count = DB::table($this->tableName)
            ->where(["model_id" => $model_id,"status" => self::STATUS_ANLE])
            ->count();
        return $count;
    }

	/**
	 * 更新
	 * @param array $aWhere
	 * @param array $aUpdate
	 * @return int
	 */
    public function editModelByWhere($aWhere, $aUpdate) {
		return DB::table($this->tableName)
            ->where($aWhere)
            ->update($aUpdate);
    }

}